package com.pm.controller;

import com.pm.common.annotation.Log;
import com.pm.common.core.controller.BaseController;
import com.pm.common.core.domain.AjaxResult;
import com.pm.common.core.page.TableDataInfo;
import com.pm.common.enums.BusinessType;
import com.pm.common.utils.poi.ExcelUtil;
import com.pm.domain.PmProject;
import com.pm.domain.Type;
import com.pm.service.IPmProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 服务项目Controller
 * 
 * @author ruoyi
 * @date 2025-09-24
 */
@RestController
@RequestMapping("/server/project")
public class PmProjectController extends BaseController
{
    @Autowired
    private IPmProjectService pmProjectService;

    /**
     * 查询服务项目列表
     */
    @PreAuthorize("@ss.hasPermi('server:project:list')")
    @GetMapping("/list")
    public TableDataInfo list(PmProject pmProject)
    {
        startPage();
        List<PmProject> list = pmProjectService.selectPmProjectList(pmProject);
        for (int i = 0; i <list.size(); i++) {
            Type type = pmProjectService.selectPmProjectTyepName(list.get(i).getTypeId());
            list.get(i).setTypeName(type.getType());
        }
        return getDataTable(list);
    }

    /**
     * 导出服务项目列表
     */
    @PreAuthorize("@ss.hasPermi('server:project:export')")
    @Log(title = "服务项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PmProject pmProject)
    {
        List<PmProject> list = pmProjectService.selectPmProjectList(pmProject);
        ExcelUtil<PmProject> util = new ExcelUtil<PmProject>(PmProject.class);
        util.exportExcel(response, list, "服务项目数据");
    }

    /**
     * 获取服务项目详细信息
     */
    @PreAuthorize("@ss.hasPermi('server:project:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pmProjectService.selectPmProjectById(id));
    }

    /**
     * 新增服务项目
     */
    @PreAuthorize("@ss.hasPermi('server:project:add')")
    @Log(title = "服务项目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PmProject pmProject)
    {
        return toAjax(pmProjectService.insertPmProject(pmProject));
    }

    /**
     * 修改服务项目
     */
    @PreAuthorize("@ss.hasPermi('server:project:edit')")
    @Log(title = "服务项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PmProject pmProject)
    {
        return toAjax(pmProjectService.updatePmProject(pmProject));
    }

    /**
     * 删除服务项目
     */
    @PreAuthorize("@ss.hasPermi('server:project:remove')")
    @Log(title = "服务项目", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pmProjectService.deletePmProjectByIds(ids));
    }
}
